var instituteInfo = $("#instituteInfo");
var section_id = $("#section_id");
var returnDateInfo = $("#returnDateInfo");
var returnDate = $("#returnDate");


$(document).ready(function() {

    disableForm("frmData");
});

function doChangeSection(para) {
    $("#section_id").find('option').remove().end();
    var params = encodeURIComponent("instituteId") + "=" + document.getElementById("institute_id").value;
    ajaxPost("?employee/getSectionList", params, function(data) {
        var response = data;
        if (response.status == false) {
            return;
        }
        $('#section_id').append(response);
        $("#section_id").val(para).attr('selected', true);
    });

}
function validateInstitute() {
    var instituteValue = $('#institute_id :selected').val();
    if (instituteValue == 0) {
        //section_id.addClass("errorField");
        instituteInfo.text("Please setect the Institute ");
        instituteInfo.addClass("errorField");
        return false;
    } else {
        instituteInfo.text(" ");
        instituteInfo.removeClass("errorField");
        return true;
    }
}
function validateDate() {
    if (returnDate.val().length < 10) {
        returnDate.addClass("errorField");
        returnDateInfo.text("Please enter return date");
        returnDateInfo.addClass("errorField");
        return false;
    }
    else {
        returnDate.removeClass("errorField");
        returnDateInfo.text("");
        returnDateInfo.removeClass("errorField");
        return true;
    }
}
function load() {
    if (validateInstitute()) {
        var params = serializeForm("frmSearch")
        ajaxPost("?dis/getCollection", params, function(data) {
            var response = JSON.parse(data);
            if (response.status == false) {
                return;
            }
            $("#formBody").html("");
            $("#formBody").html(response.tableHtml);
            $("#messagePanel").removeClass();
            $("#messagePanel").html("");
            disableForm("frmData");
            handleDefaultButtons("gridclick");
        });
    }


}
function edit() {
    enableForm("frmData");
    handleDefaultButtons("edit");
    $("#messagePanel").removeClass();
    $("#messagePanel").html("");
}
function clearBt() {
    $("#messagePanel").removeClass();
    $("#messagePanel").html("");
    $("#formBody").html("");
    initialize();
}
function save() {
    var params = serializeForm("frmData");
    ajaxPost("?dis/updateDistribution", params, function(data) {
        var response = JSON.parse(data);
        if (response.status == false) {
            return;
        }
        $("#messagePanel").removeClass();
        $("#messagePanel").addClass(response.type);
        $("#messagePanel").html(response.message);
        enableElement("btnPrint");
        enableElement("btnEdit");

        //initialize();
        // reloadGrid();

    });


    handleDefaultButtons("save");


}
function addDatePicker(item) {
    _returnDate = new dhtmlXCalendarObject([item.id]);
    _returnDate.hideTime();
}
function validateCode(item) {
    //  var reg = /^[0-9]{9}[vVxX]$/;
    // if (reg.test(nic.val())) {
    //test with db
    var input = document.getElementsByName(item).id;
    var params = encodeURIComponent("code") + "=" + item.value;
    ajaxPost("?employee/checkTldCode", params, function(data) {
        var response = JSON.parse(data);
        if (response.status == false) {
            return;
        }
        if (response.message) {
            $("#msgTest").removeClass('error');
            $("#msgTest").html("");
            return true;
        } else {

            $("#msgTest").addClass('error');
            $("#msgTest").html("Invalid Card ID: " + item.value);
            return false;
        }
    });

    /* }else{
     nic.addClass("errorField");
     nicInfo.text("Please enter valid NIC number");
     nicInfo.addClass("errorField");
     return false;
     }*/
}

function closePage() {
    window.location.href = "?home";
}
function initialize(fnAfterInit) {
    disableForm("frmData");
    handleDefaultButtons("gridclick");
    disableElement("btnPrint");
}
function onLoad() {
    initialize();
    enforcePrivileges();
    //reloadGrid();

}
onLoad();